package org.lost.base;

public class CloneDemo implements Cloneable {
    
    private String    name;
    
    private Integer   number;
    
    private CloneBean cloneBean;
    
    public static void main(String[] args) throws CloneNotSupportedException {
        CloneDemo cd = new CloneDemo();
        cd.cloneTest();
        
    }
    
    
    
    public void cloneTest() throws CloneNotSupportedException {
        //未重写 clone接口 进行测试。 
        CloneDemo d1 = new CloneDemo();
        d1.setName("d1");
        d1.setNumber(1);
        CloneBean cb1 = new CloneBean();
        cb1.setName("cb1");
        cb1.setSize(1);
        d1.setCloneBean(cb1);
        System.out.println("修改前：" + d1);
        // 使用clone方法需实现Cloneable接口，否则：CloneNotSupportedException
        CloneDemo d2 = (CloneDemo)d1.clone();
        // 潜克隆，复制对象属性引用，因此修改应用对象属性，同时被复制对象的也被修改。
        d2.getCloneBean().setName("change 2");
        
        System.out.println("d1：" + d1);
        System.out.println("d2(clone):" + d2);
    }
    
    /**
     * @return
     */
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("CloneDemo [name=");
        builder.append(name);
        builder.append(", number=");
        builder.append(number);
        builder.append(", cloneBean=");
        builder.append(cloneBean);
        builder.append("]");
        return builder.toString();
    }
    
    /**
     * @return 返回 cloneBean
     */
    public CloneBean getCloneBean() {
        return cloneBean;
    }
    
    /**
     * @param 对cloneBean进行赋值
     */
    public void setCloneBean(CloneBean cloneBean) {
        this.cloneBean = cloneBean;
    }
    
    /**
     * @return 返回 name
     */
    public String getName() {
        return name;
    }
    
    /**
     * @param 对name进行赋值
     */
    public void setName(String name) {
        this.name = name;
    }
    
    /**
     * @return 返回 number
     */
    public Integer getNumber() {
        return number;
    }
    
    /**
     * @param 对number进行赋值
     */
    public void setNumber(Integer number) {
        this.number = number;
    }
    
}
